package cn.edu.dyxy.controller;

import cn.edu.dyxy.service.UserService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @author JokerQiu
 * @date 2021年12月22日 16:52
 */
@WebServlet("/checkEmail")
public class CheckEmailServlet extends HttpServlet {

    private UserService userService = new UserService();

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        /**
         * 1、获取参数
         * 2、检查邮箱是否存在
         * 3、返回结果数据
         */
        System.out.println("CheckEmailServlet");
        req.setCharacterEncoding("UTF-8");
        //1、获取参数
        String email = req.getParameter("email");
        //2、检查
        boolean b = userService.checkEmail(email);
        //3、返回数据
        resp.setCharacterEncoding("UTF-8");
        //使用流写出我们的结果
        PrintWriter writer = resp.getWriter();
        if (b) {
            //如果b为true，说明邮箱可以用，可以被注册
            writer.write("1");
        } else {
            //邮箱已经存在在数据库中了
            writer.write("0");
        }
    }
}
